package sk.xorty.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sk.xorty.domain.Message;
import sk.xorty.service.MessageBoardService;

@Controller
@RequestMapping("/messagePost*")
public class MessagePostController {

    @Autowired
    MessageBoardService messageBoardService;

    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(Model model) {
        model.addAttribute("message", new Message());
        return "messagePost";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("message") Message message, BindingResult result) {
        if (result.hasErrors()) {
            return "messagePost";
        }
        messageBoardService.postMessage(message);
        return "redirect:messageList";
    }
}
